﻿#region using
/*
 * Created by SharpDevelop.
 * User: Andrew
 * Date: 22/04/2013
 * Time: 22:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Entities.Creatures;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Managers;
using RRRSRoguelike.Screens;

#endregion

namespace RRRSRoguelike.RuleSets
{
	/// <summary>
	/// The ruleset will contain the main game logic and communicate between the dungeonscreen and dungeon manager.
	/// </summary>
	public abstract class RuleSet
	{
		
		protected Player player;
		protected DungeonScreen dungeonScreen;
		
		// used to control monster moves
		protected bool processMonsters;
		
		public RuleSet()
		{
			
		}
		
		public virtual void Initialize()
		{
			InitializeManagers();
			
			//Instantiate a new dungeon
			GameManager.Instance.Dungeon = new Dungeon();
			
			LoadContent();
			
			player = GameManager.Instance.CreatureManager.Player;
			dungeonScreen = GameManager.Instance.DungeonScreen;
			
			AddFlags();
			ResetBools();
			
		}
		
		protected virtual void ResetBools()
		{

		}

		protected virtual void AddFlags()
		{
			// seems generic enough to add to base ruleset
			player.Flags.Add("hasMoved", false);
		}
		
		public virtual void LoadContent()
		{
			//Setup layers from data
			GameManager.Instance.Dungeon.SetupLayers();
			
			//Setup sound from data
			GameManager.Instance.Dungeon.SetupSound();
			
			//FIXME:This doesn't seem to belong here
			GameManager.Instance.PropManager.PropsToTiles();
			
			//Sets up player from Data and placed at the position given
			GameManager.Instance.CreatureManager.SetupPlayer(
				GameManager.Instance.DungeonLevel.GetValidRandomPoint(true));
			
			//FIXME: this doesn't belong here
			GameManager.Instance.CreatureManager.Player.Stats = new Stats();
			
			//FIXME: this doesn't belong here
			GameManager.Instance.DungeonLevel.CreateRandomDungeonObjects();
		}
		
		public virtual void DoLogic()
		{
			
			// does things like auto pick up sword and take stairs
			if (player.Flags["hasMoved"])
				CheckNewTile();

			if (processMonsters)
			{
				GameManager.Instance.CreatureManager.ProcessMonsters();
			}
		
		}
		
		public virtual void ProcessInput(){}
		
		protected void InitializeManagers()
		{
			GameManager.Instance.Initialize();

			ApplicationManager.Instance.GameTime.Restart();
		}
		
		protected virtual void CheckNewTile()
		{
			//FIXME: things here should be more event driven
			

			if (!GameManager.Instance.GodMode)
				//Fight monster if there is one
				GameManager.Instance.CreatureManager.CreatureCollisions();


		}
	}
}
